07. Scripting with Unity's Animator
Scripting with Unity's Animator
Project Assets
- Animator controller: Assets > UdacityVR > Art > Animations > Sun > Sun.controller
Video Addendum
At 0:38 minutes into the video
When the instructor shows the RotateLight script:
- The instructor has modified the code from the previous video.
- The GvrViewer class, used on line 14 in the video, has been removed from the GVR SDK.
- For your script to better match up with the rest of the video, we recommend that you update your script with the code provided in the Code Used in Video section below.
Important! The GvrViewer class has been removed from the GVR SDK. In general, you can use
Input.GetMouseButtonDown (0)instead ofGvrViewer.Instance.OnTriggeras shown in the code provided in the Code Used in Video section below.
At 0:55 minutes into the video
When the instructor uses the StopPlayback and StartPlayback methods:
- The StartPlayback and StopPlayback methods do not start/stop the animation, but change the mode to playback mode, which allows the use of the playbackTime value to control the current playback position.
- Because we do not need to control the current playback position, using StartPlayback and StopPlayback in this scenario is not necessarily the most obvious solution.
- If you find using StartPlayback and StopPlayback confusing, feel free to change your code as follows:
//sunRotationAnimation.StartPlayback ();
sunRotationAnimation.enabled = false; // Disable the Animator component
//sunRotationAnimation.StopPlayback ();
sunRotationAnimation.enabled = true; // Enable the Animator component
Important! The StartPlayback and StopPlayback methods do not start/stop the animation, but change the mode to playback mode.
Code Used in Video
using UnityEngine;
using System.Collections;
public class RotateLight : MonoBehaviour {
public GameObject directionalLight;
float startTime = 0f;
bool isPressed = false;
string whoRocks = "Udacity Rocks";
// Use this for initialization
void Start () {
//GvrViewer.Instance.OnTrigger += ActivateRotation;
}
// Update is called once per frame
void Update () {
Quaternion startRotation = Quaternion.Euler (50f, 30f, 0f);
Quaternion endRotation = startRotation * Quaternion.Euler (0f, 180f, 0f);
if (isPressed == true) {
directionalLight.transform.rotation = Quaternion.Slerp (startRotation, endRotation, startTime / 10f);
startTime += Time.deltaTime;
}
// Replaces 'GvrViewer.Instance.OnTrigger += ActivateRotation;'
// Typically would be placed at the top of the Update() method, but
// placed here for the script to better match up with the video
if (Input.GetMouseButtonDown (0)) {
ActivateRotation ();
}
}
public void ActivateRotation () {
isPressed = true;
}
}
Note: The string variable
string whoRocks = "Udacity Rocks";is not used but has been left in the code above because it's shown in the video. Feel free to delete or comment out that line.
Recommended Reading
- Unity Scripting API: Animator
- Unity Scripting API: Input.GetMouseButtonDown
Additional Reading
- Unity Scripting API: Animator.StartPlayback
- Unity Scripting API: Animator.StopPlayback
- Unity Scripting API: Animator.playbackTime